package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebChatDao;
import ysq.xyg.web.model.po.WebChat;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebChatVo;
import ysq.xyg.web.service.WebChatService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-聊天信息service实现
 *
 * @author shengQiang yu
 * @date 2019-9-5 11:46:49
 */
@Service("webChatService")
public class WebChatServiceImpl extends BaseServiceImpl<WebChat> implements WebChatService {

    @Resource
    private WebChatDao webChatDao;

    private WebChat webChat;

    @Autowired
    public void setBaseDao(WebChatDao webChatDao) {
        this.baseDao = webChatDao;
    }


    /**
     * 分页查询
     *
     * @param webChatVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-5 11:46:49
     */
    public ReturnEntity page(WebChatVo webChatVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebChat> list = acceptList(webChatVo);
        PageInfo<WebChat> page=new PageInfo<WebChat>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webChatVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-5 11:46:49
     */
    public ReturnEntity addOrUpdate(WebChatVo webChatVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webChatVo.getId())){
            webChat=new WebChat(currentUser);
            isUpate=false;
        }else{
            webChat=selectByPrimaryKey(webChatVo.getId());
            if(ObjectUtil.isNull(webChat)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webChat.setUpdateTime(new Date());
            webChat.setUpdateUser(currentUser.getId());
        }
        webChat.setSendId(webChatVo.getSendId());
        webChat.setReceiverId(webChatVo.getReceiverId());
        webChat.setMsgType(webChatVo.getMsgType());
        webChat.setContent(webChatVo.getContent());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webChat);
        }else{
            num=insert(webChat);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-5 11:46:49
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebChat webChat=selectByPrimaryKey(id);
        if(webChat!=null){
            webChat.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webChat.setUpdateTime(new Date());
            webChat.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webChat);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webChatVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-5 11:46:49
     */
    public List<WebChat> acceptList(WebChatVo webChatVo){
        Example example = new Example(WebChat.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webChatVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webChatVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webChatDao.selectByExample(example);
    }
    /**
     * 关联查询
     *
     * @param webChatVo 对象
     * @author shengQiang yu
     * @date 2019-04-02 12:42
     */
    @Override
    public List<WebChat> listLeft(WebChatVo webChatVo){
        return webChatDao.listLeft(webChatVo);
    }
}
